package com.yozinga.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnShowListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import com.yozinga.R;
import com.yozinga.adapter.PlacesAutoCompleteAdapter;
import com.yozinga.twitter.TwitterHomeActivity;
import com.yozinga.util.SharedData;

public class YozingaDialogFragment extends DialogFragment  implements OnItemClickListener{

	ButtonListener mButtonListener;
	AutoCompleteTextView autoCompView;
	String mTitle,mButtonLable;
	SharedData sharedData; 
	Context mContext;
	public YozingaDialogFragment() {

	}
	public YozingaDialogFragment(ButtonListener buttonListener,String title,String buttonLable,Context context) {
		mButtonListener = buttonListener;
		mTitle = title;
		mContext = context;
		mButtonLable = buttonLable;
		sharedData = new SharedData(mContext);
	}
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
	}

	/*@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			changeLocationListener = (ChangeLocationListener)activity;
		}catch(ClassCastException castException){

		}
	}*/
	public interface ButtonListener{
		public void locationValue(String locationValue);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		LayoutInflater inflater = LayoutInflater.from(getActivity());
		View dialogView = inflater.inflate(R.layout.fragment_choselocation_dialog, null);
		//etLocationDetails = (EditText)dialogView.findViewById(R.id.autocomplete);

		autoCompView = (AutoCompleteTextView) dialogView.findViewById(R.id.autocomplete);
		autoCompView.setAdapter(new PlacesAutoCompleteAdapter(getActivity(), R.layout.list_item));
		autoCompView.setOnItemClickListener(this);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
		alertDialogBuilder.setView(dialogView);
		alertDialogBuilder.setTitle(mTitle);
		alertDialogBuilder.setPositiveButton(mButtonLable,null);
		alertDialogBuilder.setNegativeButton("Cancel",null);
		final AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.setOnShowListener(new OnShowListener() {
			@Override
			public void onShow(DialogInterface dialog) {
				Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
				button.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mTitle!="twett Deal"){
							mButtonListener.locationValue(autoCompView.getText().toString());
						}else{
							sharedData.setTwitterStatus(autoCompView.getText().toString());
							startActivity(new Intent(getActivity(),TwitterHomeActivity.class));
						}
						dismiss();
					}
				});
			}
		});
		return alertDialog;
	}
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		String str = (String) adapterView.getItemAtPosition(position);
		Log.i("Today","String is  :"+str);
		mButtonListener.locationValue(str);
		//Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
		
	}
}